package com.qianyu.project.core.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.bean.Form;
import com.qianyu.project.core.bean.TargetColumn;
import com.qianyu.project.core.bean.TargetTable;
import com.qianyu.project.core.mapper.provider.FormProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author gs
 * @date create in 2020/12/11 16:27
 */
@Repository
public interface FormMapper {

    /**
     * description: 保存审批单 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [form] <br/>
     * return: java.lang.Integer
     */
    @InsertProvider(type = FormProvider.class,method = "save")
    @Options(keyProperty = "fid",keyColumn = "fid",useGeneratedKeys = true)
    void save(Form form);

    /**
     * description: 查询源表主键 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [target] <br/>
     * return: java.lang.String
     */
    @Select("select COLUMN_NAME from information_schema.COLUMNS where table_name = #{value} and table_schema='project' and COLUMN_KEY='PRI'")
    String selectPrimaryKeyColumn(String target);

    /**
     * description: 查询源表数据 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [targetTable] <br/>
     * return: java.util.Map<java.lang.String,java.lang.Object>
     */
    @Select("select * from ${table} where ${column}=#{id}")
    Map<String,Object> selectTarget(TargetTable targetTable);

    /**
     * description: 根据id查询 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [fid] <br/>
     * return: com.qianyu.flow.core.pojo.Form
     */
    @Select("select * from form where fid=#{value} and isvalid=1")
    Form selectById(Integer fid);

    /**
     * description: 根据角色查询 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [role] <br/>
     * return: java.util.List<com.qianyu.flow.core.pojo.Form>
     */
    @SelectProvider(type = FormProvider.class,method = "selectByRole")
    List<Form> selectByRole(String role);

    /**
     * description: 更新 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [form] <br/>
     * return: void
     */
    @Update("update form set current=#{current},status=#{status} where fid=#{fid}")
    void update(Form form);

    /**
     * description: 根据用户查询 <br/>
     * author: gs   <br/>
     * date: 2020/12/12 <br/>
     * params: [eid] <br/>
     * return: java.util.List<com.qianyu.flow.core.pojo.Form>
     */
    @SelectProvider(type = FormProvider.class,method = "selectByUser")
    List<Form> selectByUser(Long id);

    @Select("select column_name,column_comment from information_schema.COLUMNS where table_name = #{value} and table_schema='project'")
    List<TargetColumn> selectTargetColumns(String target);

    /*
     * description: 查询当前登录人待办事项 <br/>
     * author: mfy   <br/>
     * date:  <br/>
     * params:  <br/>
     * return:
     */
    @SelectProvider(type = FormProvider.class,method = "selectMybacklog")
    IPage<Form> selectMybacklog(IPage page,Map<String,String> params);

    @Select("select * from form fo,flow fl where fo.flowId=fl.flowId and fo.eid=#{value}")
    List<Form> zhuyedeshenqing(Long id);

    @Select("update form set isvalid=0 where fid in (${value})")
    void deleteById(String fid);
}
